package com.aigou.admin.dao.impl;

import com.aigou.admin.dao.ProductDAO;
import com.aigou.admin.dao.UserDAO;
import com.aigou.admin.entity.Product;
import com.aigou.admin.entity.User;
import com.aigou.admin.util.JDBCUtils;

import java.util.List;

/**
 * 数据接口实现类
 */
public class ProductDAOImpl implements ProductDAO {

    @Override
    public List<Product> selectAll() {
        List<Product> products = JDBCUtils.executeQuery(Product.class,"select * from s_product");
        return products;
    }

    @Override
    public Product selectById(String productId) {
        List<Product> products = JDBCUtils.executeQuery(Product.class,"select * from s_product where id = ?",productId);
        if(products.isEmpty()){
            return null;
        }
        return products.get(0);
    }

    @Override
    public boolean insert(Product product) {
        int rows = JDBCUtils.executeUpdate("insert into s_product(id,product_name,product_image,price,product_type,product_desc,create_time,product_brand) values(?,?,?,?,?,?,?,?)",
                product.getId(),product.getProduct_name(),product.getProduct_image(),product.getPrice(),product.getProduct_type(),product.getProduct_desc(),product.getCreate_time(),product.getProduct_brand());
        return rows > 0;
    }

    @Override
    public boolean delete(String productId) {
        int rows = JDBCUtils.executeUpdate("delete from  s_product where id = ?",productId);
        return rows > 0;
    }

    @Override
    public boolean update(Product product) {
        int rows = JDBCUtils.executeUpdate("update s_product set product_name=?,product_image=?,price=?,product_type=?,product_desc=?,create_time=?,product_brand=? where id=?",
                product.getProduct_name(),product.getProduct_image(),product.getPrice(),product.getProduct_type(),product.getProduct_desc(),product.getCreate_time(),product.getProduct_brand(),product.getId());
        return rows > 0;
    }
}
